package cn.trasen.his.poc.std.core.action;

import cn.trasen.his.poc.std.api.emums.BedStatusEnum;
import cn.trasen.his.poc.std.api.emums.InpatientStatusEnum;
import cn.trasen.his.poc.std.api.model.inpatient.InpatientRecord;
import cn.trasen.his.poc.std.api.model.major.WardBed;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import pro.shushi.pamirs.framework.connectors.data.sql.Pops;
import pro.shushi.pamirs.meta.annotation.Action;
import pro.shushi.pamirs.meta.annotation.Function;
import pro.shushi.pamirs.meta.annotation.Model;
import pro.shushi.pamirs.meta.api.session.PamirsSession;
import pro.shushi.pamirs.meta.constant.ExpConstants;
import pro.shushi.pamirs.meta.constant.FunctionConstants;
import pro.shushi.pamirs.meta.enmu.ActionContextTypeEnum;
import pro.shushi.pamirs.meta.enmu.ViewTypeEnum;

import java.util.Date;

@Component
@Model.model(InpatientRecord.MODEL_MODEL)
public class InpatientRecordAction {

    @Transactional
    @Action.Advanced(name = FunctionConstants.create, managed = true, invisible = ExpConstants.idValueExist)
    @Action(displayName = "保存", summary = "创建", bindingType = ViewTypeEnum.FORM)
    @Function(name = FunctionConstants.create)
    @Function.fun(FunctionConstants.create)
    public InpatientRecord create(InpatientRecord data) {
        data.setStatus(InpatientStatusEnum.SIGN_NEW_IN_HOSP);
        data.construct();
        data.create();
        data.fieldSave(InpatientRecord::getInpatientHl);
        data.fieldSave(InpatientRecord::getInpatientFollow);

        new WardBed().updateByWrapper(new WardBed().setBedStatus(BedStatusEnum.OCCUPYBED),
                Pops.<WardBed>lambdaUpdate().from(WardBed.MODEL_MODEL).eq(WardBed::getBedNo, data.getBedNo()));

        return data;
    }

    @Transactional
    @Action(displayName = "入区", bindingType = ViewTypeEnum.TABLE, contextType = ActionContextTypeEnum.SINGLE)
    @Action.Advanced(invisible = "activeRecord.status != 'SIGN_NEW_IN_HOSP'")
    public InpatientRecord entryWard(InpatientRecord record) {
        InpatientRecord dbRecord = new InpatientRecord().setId(record.getId()).queryById();

        InpatientRecord upRecord = new InpatientRecord();
        upRecord.setId(record.getId());
        upRecord.setStatus(InpatientStatusEnum.SIGN_IN_AREA);
        upRecord.setInAreaDate(new Date());
        upRecord.updateById();

        new WardBed().updateByWrapper(new WardBed().setBedStatus(BedStatusEnum.SOMEONE),
                Pops.<WardBed>lambdaUpdate().from(WardBed.MODEL_MODEL).eq(WardBed::getBedNo, dbRecord.getBedNo()));

        PamirsSession.getMessageHub().info("操作「入区」成功");
        return record;
    }

    @Transactional
    @Action(displayName = "取消入院", bindingType = ViewTypeEnum.TABLE, contextType = ActionContextTypeEnum.SINGLE)
    @Action.Advanced(invisible = "activeRecord.status != 'SIGN_NEW_IN_HOSP'")
    public InpatientRecord cancelInHosp(InpatientRecord record) {
        InpatientRecord dbRecord = new InpatientRecord().setId(record.getId()).queryById();

        InpatientRecord upRecord = new InpatientRecord();
        upRecord.setId(record.getId());
        upRecord.setStatus(InpatientStatusEnum.SIGN_CANCEL_IN_HOSP);
        upRecord.updateById();

        new WardBed().updateByWrapper(new WardBed().setBedStatus(BedStatusEnum.EMPTYBED),
                Pops.<WardBed>lambdaUpdate().from(WardBed.MODEL_MODEL).eq(WardBed::getBedNo, dbRecord.getBedNo()));

        PamirsSession.getMessageHub().info("操作「取消入院」成功");
        return record;
    }

    @Transactional
    @Action(displayName = "出区", bindingType = ViewTypeEnum.TABLE, contextType = ActionContextTypeEnum.SINGLE)
    @Action.Advanced(invisible = "activeRecord.status != 'SIGN_IN_AREA'")
    public InpatientRecord outWard(InpatientRecord record) {
        InpatientRecord dbRecord = new InpatientRecord().setId(record.getId()).queryById();

        InpatientRecord upRecord = new InpatientRecord();
        upRecord.setId(record.getId());
        upRecord.setStatus(InpatientStatusEnum.SIGN_OUT_AREA);
        upRecord.setOutAreaDate(new Date());
        upRecord.updateById();

        new WardBed().updateByWrapper(new WardBed().setBedStatus(BedStatusEnum.EMPTYBED),
                Pops.<WardBed>lambdaUpdate().from(WardBed.MODEL_MODEL).eq(WardBed::getBedNo, dbRecord.getBedNo()));

        PamirsSession.getMessageHub().info("操作「出区」成功");
        return record;
    }

}
